package com.wenlong.train_ticketing_system_springboot.querymodule.controller;

import com.wenlong.train_ticketing_system_springboot.querymodule.pojo.Order;
import com.wenlong.train_ticketing_system_springboot.querymodule.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 苏文龙
 * @version 1.0
 */
@RestController
@RequestMapping("/trainT")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/payForOrder")
    public List<Order> payForOrder(@RequestParam("id") Long id){
        Order order = orderService.getById(id);
        order.setStatus(1);
        orderService.updateById(order);
        String proId = (String) redisTemplate.opsForValue().getAndDelete("" + id);
        System.out.println("删除 redis 中 proId 为" + proId + "值");
        List<Order> orders =  orderService.getAllOrders();
        return orders;
    }

    @RequestMapping("/getAllOrders")
    public List<Order> getAllOrders(){
        List<Order> orders =  orderService.getAllOrders();
        return orders;
    }

    @RequestMapping("/testReturn")
    public String testReturn(){
        return "this test has perfect result!!";
    }
}
